package com.android.zhushou.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.android.zhushou.R;
import com.android.zhushou.adapter.InterviewAdapter;
import com.android.zhushou.base.BasePutToRefreshFragment;
import com.android.zhushou.cache.ReadCacheAsyncTask;
import com.android.zhushou.entity.Interview;
import com.android.zhushou.ui.activites.InterviewDetailActivity;
import com.android.zhushou.ui.activites.MainActivity;
import com.android.zhushou.ui.view.LoadingLayout;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

/**
 * 面试问题fragment
 */
public class InterviewFragment extends BasePutToRefreshFragment<InterviewAdapter> {
    private InterviewAdapter adapter;

    @Override
    public View getRootView() {
        return LayoutInflater.from(getActivity()).inflate(R.layout.fragment_review, null, false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MainActivity aty = (MainActivity) getActivity();
        aty.setTitleName("面试录");
        setLoadMore(true);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public InterviewAdapter getAdapter() {
        adapter = new InterviewAdapter(getActivity());
        return adapter;
    }

    @Override
    public void readCache() {//读取缓存操作
        ReadCacheAsyncTask<ArrayList<Interview>> readCache = new ReadCacheAsyncTask(getActivity());
        readCache.setOnReadCacheToDo(new ReadCacheAsyncTask.OnReadCacheToDo<ArrayList<Interview>>() {
            @Override
            public void preExecute() {
                mLoadingLayout.setLoadingLayout(LoadingLayout.NETWORK_LOADING);
                mPtrFrameLayout.setVisibility(View.GONE);
            }

            @Override
            public void postExectue(ArrayList<Interview> data) {
                if (data == null || data.size() == 0) {
                    mLoadingLayout.setLoadingLayout(LoadingLayout.NETWORK_REFRESH);
                    mPtrFrameLayout.autoRefresh(true);
                } else {


                }
            }
        });

        readCache.execute("mianshi_timu");
    }

    @Override
    public void requestDataByNet(final int actionType) {//读取网络数据
        BmobQuery<Interview> query = new BmobQuery<Interview>();
        if (actionType != 0) {
            REFRESH_TYPE_LOAD_MORE++;
            query.setSkip(actionType * 11 + 1);
        } else {
            REFRESH_TYPE_LOAD_MORE = 1;
            query.setSkip(0);
        }
        query.setLimit(11);
        query.findObjects(getActivity(), new FindListener<Interview>() {
            @Override
            public void onSuccess(List<Interview> list) {
                if (list.size() == 0) {//加载完成
                    sPutUpState = PULL_UP_STATE_NOMORE;
                    updateFooterViewStateText();
                    return;
                }
                if (actionType == 0) {//刷新数据
                    mAdapter.notifyAdapter(list);
                } else {
                    mAdapter.addData(list);
                }

                mPtrFrameLayout.setVisibility(View.VISIBLE);
                mPtrFrameLayout.refreshComplete();
                removeFootView();
            }

            @Override
            public void onError(int i, String s) {
            }
        });
    }


    @Override
    public void initArguments() {

    }

    @Override
    public void createViewsOrListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                Interview data = (Interview) parent.getItemAtPosition(position);
                Intent intent = new Intent(getActivity(), InterviewDetailActivity.class);
                intent.putExtra("title", data.getQuestion());
                intent.putExtra("content", data.getAnswer());
                getActivity().startActivity(intent);

            }
        });
    }


}
